package com.jeeplus.modules.api;


import com.alibaba.fastjson.JSONObject;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.config.properties.JeePlusProperites;
import com.jeeplus.core.persistence.BaseEntity;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.aboutus.service.AboutUsService;
import com.jeeplus.modules.address.entity.Address;
import com.jeeplus.modules.address.service.AddressService;
import com.jeeplus.modules.aboutus.entity.AboutUs;
import com.jeeplus.modules.agreement.entity.Agreement;
import com.jeeplus.modules.agreement.service.AgreementService;
import com.jeeplus.modules.ambassadorrewards.entity.AmbassadorRewards;
import com.jeeplus.modules.ambassadorrewards.service.AmbassadorRewardsService;
import com.jeeplus.modules.api.common.ReqJson;
import com.jeeplus.modules.api.common.UUIDUtils;
import com.jeeplus.modules.api.utils.AliSms;
import com.jeeplus.modules.api.utils.KuaidiService;
import com.jeeplus.modules.banner.entity.Banner;
import com.jeeplus.modules.banner.service.BannerService;
import com.jeeplus.modules.customerservice.entity.CustomerService;
import com.jeeplus.modules.customerservice.service.CustomerServiceService;
import com.jeeplus.modules.express.entity.Express;
import com.jeeplus.modules.express.service.ExpressService;
import com.jeeplus.modules.faq.entity.Faq;
import com.jeeplus.modules.faq.service.FaqService;
import com.jeeplus.modules.feedback.entity.Feedback;
import com.jeeplus.modules.feedback.service.FeedbackService;
import com.jeeplus.modules.goods.entity.Goods;
import com.jeeplus.modules.goods.service.GoodsService;
import com.jeeplus.modules.hotbroadcast.entity.HotBroadcast;
import com.jeeplus.modules.hotbroadcast.service.HotBroadcastService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import com.jeeplus.modules.memberorder.entity.MemberOrder;
import com.jeeplus.modules.memberorder.service.MemberOrderService;
import com.jeeplus.modules.photo.entity.Photo;
import com.jeeplus.modules.photo.service.PhotoService;
import com.jeeplus.modules.point.entity.Point;
import com.jeeplus.modules.point.service.PointService;
import com.jeeplus.modules.share.entity.Share;
import com.jeeplus.modules.share.service.ShareService;
import com.jeeplus.modules.version.entity.Version;
import com.jeeplus.modules.version.service.VersionService;
import com.jeeplus.modules.withdraw.entity.Withdraw;
import com.jeeplus.modules.withdraw.service.WithdrawService;
import com.jeeplus.modules.withdrawinfo.entity.WithdrawInfo;
import com.jeeplus.modules.withdrawinfo.service.WithdrawInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
@RequestMapping("/api/user/")
@Slf4j
public class AppController extends BaseController {

    private final AgreementService agreementService;
    private final MemberService memberService;
    private final BannerService bannerService;
    private final HotBroadcastService hotBroadcastService;
    private final GoodsService goodsService;
    private final CustomerServiceService customerServiceService;
    private final AddressService addressService;
    private final MemberOrderService memberOrderService;
    private final ExpressService expressService;
    private final VersionService versionService;
    private final FeedbackService feedbackService;
    private final AboutUsService aboutUsService;
    private final FaqService faqService;
    private final PhotoService photoService;
    private final PointService pointService;
    private final WithdrawService withdrawService;
    private final ShareService shareService;
    private final WithdrawInfoService withdrawInfoService;
    private final AmbassadorRewardsService ambassadorRewardsService;
    private final RedisTemplate<String, String> redisTemplate;

    public AppController(AgreementService agreementService, MemberService memberService, BannerService bannerService, HotBroadcastService hotBroadcastService, GoodsService goodsService, CustomerServiceService customerServiceService, AddressService addressService, MemberOrderService memberOrderService, ExpressService expressService, VersionService versionService, FeedbackService feedbackService, AboutUsService aboutUsService, FaqService faqService, PhotoService photoService, PointService pointService, WithdrawService withdrawService, ShareService shareService, WithdrawInfoService withdrawInfoService, AmbassadorRewardsService ambassadorRewardsService, RedisTemplate<String, String> redisTemplate) {
        this.agreementService = agreementService;
        this.memberService = memberService;
        this.bannerService = bannerService;
        this.hotBroadcastService = hotBroadcastService;
        this.goodsService = goodsService;
        this.customerServiceService = customerServiceService;
        this.addressService = addressService;
        this.memberOrderService = memberOrderService;
        this.expressService = expressService;
        this.versionService = versionService;
        this.feedbackService = feedbackService;
        this.aboutUsService = aboutUsService;
        this.faqService = faqService;
        this.photoService = photoService;
        this.pointService = pointService;
        this.withdrawService = withdrawService;
        this.shareService = shareService;
        this.withdrawInfoService = withdrawInfoService;
        this.ambassadorRewardsService = ambassadorRewardsService;
        this.redisTemplate = redisTemplate;
    }

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


    /**
     * 上传单张图片返回链接
     *
     * @return ajaxJson
     */
    @RequestMapping("uploadFile")
    public AjaxJson addImages(@RequestParam(value = "file", required = false) MultipartFile[] file) {
        AjaxJson res = new AjaxJson();
        res.setMsg("上传失败");
        try {
            List<String> list = new ArrayList<>();
            if (file != null && file.length > 0) {
                for (MultipartFile multipartFile : file) {
                    String realPath = JeePlusProperites.newInstance().getUserfilesBaseDir() + "userfiles/1/files/authimage";
                    // String dateDir = DateTimeUtil.dateConvtoFmt(new Date(),
                    // "yyyyMM"); // 按日期存放上传的文件
                    String[] split = DateUtils.formatDate(new Date(), "yyyy-MM").split("-");
                    String dateDir = split[0] + "/" + split[1];
                    File path = new File(realPath + File.separator + dateDir);
                    if (!path.exists()) {
                        path.mkdirs();
                    }
                    String filename1 = multipartFile.getOriginalFilename();
                    String ext1 = null;
                    if (filename1 != null) {
                        ext1 = filename1.substring(filename1.lastIndexOf(".")).toLowerCase();
                    }
                    String fileName1 = DateUtils.formatDate(new Date(), "yyyyMMddHHmmss")
                            + RandomStringUtils.randomNumeric(4) + ext1;
                    File localFile1 = new File(path.getAbsolutePath() + File.separator + fileName1);
                    multipartFile.transferTo(localFile1);
                    list.add(filePath + "/userfiles/1/files/authimage/" + dateDir + "/" + fileName1);
                }
            }
            res.put("dataList", list);
            res.setCode(0);
            res.setMsg("提交成功");
        } catch (Exception e) {
            log.error("请求失败:{}", e);
        }
        return res;
    }

    /**
     * 协议获取
     *
     * @param req 参数
     * @return ajaxJson
     */
    @RequestMapping("getAgree")
    public AjaxJson findAgreement(@RequestBody ReqJson req) {
        AjaxJson res = new AjaxJson();
        if (StringUtils.isBlank(req.getString("type"))) {
            res.setMsg("TYPE类型不能为空！");
            return res;
        }
        Agreement info = agreementService.findUniqueByProperty("type", req.getString("type"));
        if (info == null) {
            res.setMsg("信息不存在！");
            return res;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("content", StringUtils.isBlank(info.getContent()) ? "" : info.getContent());
        res.put("data", map);
        res.setCode(0);
        res.setMsg("查询成功");
        return res;
    }

    /**
     * 请求验证码
     *
     * @param req 手机号
     * @return 验证码
     */
    @RequestMapping("requestMsg")
    public AjaxJson requestMsg(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String mobile = req.getString("mobile");
        if (StringUtils.isBlank(mobile)) {
            ajaxJson.setMsg("手机号不能为空");
            return ajaxJson;
        }
        // 生成6位验证码
        String uuid = UUIDUtils.uuid(6);
        logger.info("验证码:{}", uuid);
        // 发送验证码
        AliSms.sendCode(mobile, uuid);
        // 将验证码有效期设置为5分钟
        redisTemplate.opsForValue().set(mobile, uuid, 5, TimeUnit.MINUTES);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }


    /**
     * 忘记密码
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("resetPsd")
    public AjaxJson resetPsd(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String mobile = req.getString("mobile");
        String code = req.getString("code");
        String passwd = req.getString("passwd");
        if (StringUtils.isBlank(mobile)) {
            ajaxJson.setMsg("手机号不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(code)) {
            ajaxJson.setMsg("验证码不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(passwd)) {
            ajaxJson.setMsg("密码不能为空");
            return ajaxJson;
        }
        // 通过手机号查询该用户是否存在
        Member mobile1 = getMemberByMobile(mobile);
        if (Objects.isNull(mobile1)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        // 判断账户的启用状态
        if (mobile1.getState().equals("0")) {
            ajaxJson.setMsg("操作失败,账户已冻结");
            return ajaxJson;
        }
        // 查询验证码是否正确
        String s = redisTemplate.opsForValue().get(mobile);
        if (StringUtils.isBlank(s) || !s.equals(code)) {
            ajaxJson.setMsg("手机号或验证码错误");
            return ajaxJson;
        }
        // 设置新密码
        mobile1.setPasswd(passwd);
        memberService.save(mobile1);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("操作成功");
        return ajaxJson;
    }

    /**
     * 通过手机号查询用户
     * @param mobile 手机号
     * @return 用户
     */
    private Member getMemberByMobile(String mobile) {
        return memberService.findUniqueByProperty("mobile", mobile);
    }

    /**
     * 用户注册
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("register")
    public AjaxJson register(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String mobile = req.getString("mobile");
        String code = req.getString("code");
        String passwd = req.getString("passwd");
        String referrerMobile = req.getString("referrerMobile");
        if (StringUtils.isBlank(mobile)) {
            ajaxJson.setMsg("手机号不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(referrerMobile)) {
            ajaxJson.setMsg("推荐码不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(code)) {
            ajaxJson.setMsg("验证码不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(passwd)) {
            ajaxJson.setMsg("密码不能为空");
            return ajaxJson;
        }
        // 通过手机号查询该用户是否存在
        Member mobile1 = getMemberByMobile(mobile);
        if (Objects.nonNull(mobile1)) {
            ajaxJson.setMsg("该手机号已存在, 请登录");
            return ajaxJson;
        }
        // 查询验证码是否正确
        String s = redisTemplate.opsForValue().get(mobile);
        if (StringUtils.isBlank(s) || !s.equals(code)) {
            ajaxJson.setMsg("手机号或验证码错误");
            return ajaxJson;
        }
        // 新用户添加
        Member member = new Member();
        member.setNickname("用户" + mobile.substring(7));
        member.setMobile(mobile);
        member.setVipType("1");
        // 密码加密存储
        member.setPasswd(passwd);
        // 数据初始化
        member.setTcPoint("0");
        member.setFgPoint("0");
        member.setState("1");
        member.setOrderCount(0);
        member.setSellCount(0);
        member.setBoxCount(0);
        member.setMasterReward("0.00");
        member.setQrCode(IdGen.uuid().substring(0, 10));
        // 查询默认头像
        Page<Photo> page = photoService.findPage(new Page<>(1, 1), new Photo());
        Photo photo = page.getList().get(0);
        member.setPhoto(photo.getPhoto());
        // 用户保存
        memberService.save(member);
        memberService.updatePoint(member);
        // 分享记录添加
        Share share = new Share();
        share.setU(member);
        shareService.save(share);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("操作成功");
        return ajaxJson;
    }

    /**
     * 用户登录
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("login")
    public AjaxJson login(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String mobile = req.getString("mobile");
        String passwd = req.getString("passwd");
        if (StringUtils.isBlank(mobile)) {
            ajaxJson.setMsg("手机号不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(passwd)) {
            ajaxJson.setMsg("密码不能为空");
            return ajaxJson;
        }
        Member member = getMemberByMobile(mobile);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在, 请先注册");
            return ajaxJson;
        }
        // 密码校验
        if (!passwd.equals(member.getPasswd())) {
            ajaxJson.setMsg("手机号或密码不正确");
            return ajaxJson;
        }
        // 判断账户的启用状态
        if (member.getState().equals("0")) {
            ajaxJson.setMsg("登录失败,账户已冻结");
            return ajaxJson;
        }
        // 登录成功返回参数
        Map<String, Object> data = new ConcurrentHashMap<>();
        data.put("uid", member.getId());
        data.put("mobile", member.getMobile());
        data.put("vipType", member.getVipType());
        ajaxJson.put("data", data);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 轮播图列表
     *
     * @return 状态
     */
    @RequestMapping("bannerList")
    public AjaxJson bannerList() {
        AjaxJson ajaxJson = new AjaxJson();
        Banner banner = new Banner();
        Page<Banner> page = new Page<>();
        page.setOrderBy(" a.sort asc");
        banner.setPage(page);
        List<Banner> list = bannerService.findList(banner);
        List<Map<String, Object>> mapList = new CopyOnWriteArrayList<>();
        for (Banner banner1 : list) {
            Map<String, Object> map = new ConcurrentHashMap<>();
            map.put("id", banner1.getId());
            map.put("img", getRealPath(banner1.getImg()));
            map.put("jumpType", banner1.getJumpType());
            map.put("goodsId", Objects.isNull(banner1.getGoods()) ? "" : banner1.getGoods().getId());
            map.put("content", StringUtils.isBlank(banner1.getContent()) ? "" : banner1.getContent());
            mapList.add(map);
        }
        ajaxJson.put("data", mapList);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 热点播报
     *
     * @return 状态
     */
    @RequestMapping("hotList")
    public AjaxJson hotList() {
        AjaxJson ajaxJson = new AjaxJson();
        HotBroadcast hotBroadcast = new HotBroadcast();
        Page<HotBroadcast> page = new Page<>();
        page.setOrderBy(" a.sort asc");
        hotBroadcast.setPage(page);
        List<HotBroadcast> list = hotBroadcastService.findList(hotBroadcast);
        List<Map<String, Object>> mapList = new CopyOnWriteArrayList<>();
        for (HotBroadcast hotBroadcast1 : list) {
            Map<String, Object> map = new ConcurrentHashMap<>();
            map.put("id", hotBroadcast1.getId());
            map.put("title", hotBroadcast1.getTitle());
            mapList.add(map);
        }
        ajaxJson.put("data", mapList);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 热点详情
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("hotInfo")
    public AjaxJson hotInfo(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String hotId = req.getString("hotId");
        if (StringUtils.isBlank(hotId)) {
            ajaxJson.setMsg("热点id不能为空");
            return ajaxJson;
        }
        HotBroadcast hotBroadcast = hotBroadcastService.get(hotId);
        if (Objects.isNull(hotBroadcast)) {
            ajaxJson.setMsg("该热点不存在");
            return ajaxJson;
        }
        ajaxJson.put("data", hotBroadcast.getContent());
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 商品列表
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("goodsList")
    public AjaxJson goodsList(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        Integer pageNo = req.getInteger("pageNo");
        Integer pageSize = req.getInteger("pageSize");
        if (Objects.isNull(pageNo) || Objects.isNull(pageSize)) {
            ajaxJson.setMsg("分页参数不能为空");
            return ajaxJson;
        }
        Page<Goods> page = new Page<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrderBy(" a.sort asc");
        Page<Goods> page1 = goodsService.findPage(page, new Goods());
        Map<String, Object> data = new ConcurrentHashMap<>();
        data.put("total", page1.getLast());
        data.put("count", page1.getCount());
        List<Map<String, Object>> dataList = new CopyOnWriteArrayList<>();
        for (Goods goods : page1.getList()) {
            Map<String, Object> map = new ConcurrentHashMap<>();
            map.put("id", goods.getId());
            map.put("icon", getRealPath(goods.getIcon()));
            map.put("title", goods.getTitle());
            map.put("quantity", goods.getQuantity());
            map.put("price", goods.getPrice());
            dataList.add(map);
        }
        data.put("dataList", dataList);
        ajaxJson.put("data", data);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 商品详情
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("goodsInfo")
    public AjaxJson goodsInfo(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String goodsId = req.getString("goodsId");
        String uid = req.getString("uid");
        if (StringUtils.isBlank(goodsId)) {
            ajaxJson.setMsg("商品id不能为空");
            return ajaxJson;
        }
        Goods goods = goodsService.get(goodsId);
        if (Objects.isNull(goods)) {
            ajaxJson.setMsg("该商品不存在");
            return ajaxJson;
        }
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        Map<String, Object> map = new ConcurrentHashMap<>();
        map.put("id", goods.getId());
        map.put("img", convertImageList(goods.getBanner()));
        map.put("title", goods.getTitle());
        map.put("quantity", goods.getQuantity());
        map.put("content", goods.getContent());
        ajaxJson.put("data", map);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 联系客服
     *
     * @return 状态
     */
    @RequestMapping("contactCusService")
    public AjaxJson contactCusService() {
        AjaxJson ajaxJson = new AjaxJson();
        Map<String, Object> map = new ConcurrentHashMap<>();
        List<CustomerService> list = customerServiceService.findList(new CustomerService());
        if (CollectionUtils.isEmpty(list)) {
            ajaxJson.setMsg("未查询到相关信息");
            return ajaxJson;
        }
        CustomerService customerService = list.get(0);
        map.put("phone", customerService.getPhone());
        map.put("startTime", customerService.getStartTime());
        map.put("endTime", customerService.getEndTime());
        map.put("wechat", customerService.getWechat());
        ajaxJson.put("data", map);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 获取用户地址
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("getAddrList")
    public AjaxJson getAddrList(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String uid = req.getString("uid");
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        Address address = new Address();
        address.setU(member);
        List<Address> list = addressService.findList(address);
        List<Map<String, Object>> data = new CopyOnWriteArrayList<>();
        for (Address address1 : list) {
            Map<String, Object> map = new ConcurrentHashMap<>();
            map.put("id", address1.getId());
            map.put("linkman", address1.getLinkman());
            map.put("mobile", address1.getMobile());
            map.put("state", address1.getState());
            map.put("address", address1.getAddress());
            map.put("addressDetail", address1.getAddressDetail());
            data.add(map);
        }
        ajaxJson.put("data", data);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 获取默认地址
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("getDefaultAddress")
    public AjaxJson getDefaultAddress(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String uid = req.getString("uid");
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        Address address = new Address();
        address.setU(member);
        address.setState("1");
        List<Address> list = addressService.findList(address);
        if (CollectionUtils.isEmpty(list)) {
            ajaxJson.setCode(0);
            ajaxJson.setMsg("默认地址未设置");
            return ajaxJson;
        }
        Address address1 = list.get(0);
        Map<String, Object> map = new ConcurrentHashMap<>();
        map.put("id", address1.getId());
        map.put("linkman", address1.getLinkman());
        map.put("mobile", address1.getMobile());
        map.put("state", address1.getState());
        map.put("address", address1.getAddress());
        map.put("addressDetail", address1.getAddressDetail());
        ajaxJson.put("data", map);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("获取成功");
        return ajaxJson;
    }

    /**
     * 地址详情
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("addrInfo")
    public AjaxJson addrInfo(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String addrId = req.getString("addrId");
        if (StringUtils.isBlank(addrId)) {
            ajaxJson.setMsg("地址id不能为空");
            return ajaxJson;
        }
        Address address = addressService.get(addrId);
        if (Objects.isNull(address)) {
            ajaxJson.setMsg("地址id不能为空");
            return ajaxJson;
        }
        Map<String, Object> map = new ConcurrentHashMap<>();
        map.put("id", address.getId());
        map.put("linkman", address.getLinkman());
        map.put("mobile", address.getMobile());
        map.put("state", address.getState());
        map.put("address", address.getAddress());
        map.put("addressDetail", address.getAddressDetail());
        ajaxJson.put("data", map);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 添加或编辑地址
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("editAddr")
    public AjaxJson editAddr(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String id = req.getString("id");
        String uid = req.getString("uid");
        String linkman = req.getString("linkman");
        String mobile = req.getString("mobile");
        String address = req.getString("address");
        String addressDetail = req.getString("addressDetail");
        String state = req.getString("state");
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        if (state.equals("1")) {
            Address address1 = new Address();
            address1.setU(member);
            address1.setState("1");
            List<Address> list = addressService.findList(address1);
            for (Address address2 : list) {
                address2.setState("0");
                addressService.updateState(address2);
            }
        }
        Address address1 = new Address();
        address1.setU(member);
        address1.setLinkman(linkman);
        address1.setState(state);
        address1.setMobile(mobile);
        address1.setId(id);
        address1.setAddress(address);
        address1.setAddressDetail(addressDetail);
        if (StringUtils.isBlank(id)) {
            addressService.save(address1);
        } else {
            addressService.updateState(address1);
        }
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 地址删除
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("delAddr")
    public AjaxJson delAddr(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String addrId = req.getString("addrId");
        if (StringUtils.isBlank(addrId)) {
            ajaxJson.setMsg("地址id不能为空");
            return ajaxJson;
        }
        addressService.delete(new Address(addrId));
        ajaxJson.setCode(0);
        ajaxJson.setMsg("操作成功");
        return ajaxJson;
    }

    /**
     * 创建订单
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("createOrder")
    public AjaxJson createOrder(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String uid = req.getString("uid");
        String goodsId = req.getString("goodsId");
        Integer quantity = req.getInteger("quantity");
        String money = req.getString("money");
        JSONObject address = req.getJSONObject("address");
        String remark = req.getString("remark");
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        // 判断账户的启用状态
        if (member.getState().equals("0")) {
            ajaxJson.setMsg("操作失败,账户已冻结");
            return ajaxJson;
        }
        if (StringUtils.isBlank(goodsId)) {
            ajaxJson.setMsg("商品id不能为空");
            return ajaxJson;
        }
        Goods goods = goodsService.get(goodsId);
        if (Objects.isNull(goods)) {
            ajaxJson.setMsg("该商品不存在");
            return ajaxJson;
        }
        if (Objects.isNull(quantity)) {
            ajaxJson.setMsg("数量不能为空");
            return ajaxJson;
        }
        if (Objects.isNull(money)) {
            ajaxJson.setMsg("金额不能为空");
            return ajaxJson;
        }
        if (Objects.isNull(address)) {
            ajaxJson.setMsg("地址不能为空");
            return ajaxJson;
        }
        MemberOrder memberOrder = new MemberOrder();
        memberOrder.setU(member);
        memberOrder.setPayState("1");
        memberOrder.setIsNewRecord(true);
        memberOrder.setId(UUIDUtils.orderUuid(6));
        memberOrder.setGoods(goods);
        memberOrder.setTitle(goods.getTitle());
        memberOrder.setIcon(goods.getIcon());
        memberOrder.setPrice(goods.getPrice());
        memberOrder.setQuantity(quantity);
        memberOrder.setBoxNum(quantity * goods.getQuantity());
        memberOrder.setMoney(new BigDecimal(money).setScale(2, RoundingMode.HALF_UP).toString());
        memberOrder.setAddress(address.toString());
        memberOrder.setOrderState("1");
        memberOrder.setPayState("0");
        memberOrder.setRemark(remark);
        memberOrderService.save(memberOrder);
        Map<String, Object> map = new ConcurrentHashMap<>();
        map.put("orderId", memberOrder.getId());
        map.put("payPsdState", StringUtils.isBlank(member.getPayPasswd()) ? "0" : "1");
        map.put("tcPoint", member.getTcPoint());
        map.put("fgPoint", member.getFgPoint());
        ajaxJson.put("data", map);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("下单成功");
        return ajaxJson;
    }



    /**
     * 订单支付
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("orderPay")
    public AjaxJson orderPay(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String orderId = req.getString("orderId");
        String payPsd = req.getString("payPsd");
        if (StringUtils.isBlank(orderId)) {
            ajaxJson.setMsg("订单号不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(payPsd)) {
            ajaxJson.setMsg("密码不能为空");
            return ajaxJson;
        }
        MemberOrder memberOrder = memberOrderService.get(orderId);
        if (Objects.isNull(memberOrder)) {
            ajaxJson.setMsg("该订单不存在");
            return ajaxJson;
        }
        // 查询订单中的用户
        Member member = memberService.get(memberOrder.getU());
        if (!member.getPayPasswd().equals(payPsd)) {
            ajaxJson.setMsg("支付失败，密码不正确");
            return ajaxJson;
        }
        // 判断账户的启用状态
        if (member.getState().equals("0")) {
            ajaxJson.setMsg("操作失败,账户已冻结");
            return ajaxJson;
        }
        // 判断该用户的积分
        if (new BigDecimal(memberOrder.getMoney()).compareTo(new BigDecimal(member.getTcPoint()).add(new BigDecimal(member.getFgPoint()))) > 0) {
            ajaxJson.setMsg("余额不足");
            return ajaxJson;
        }
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 设置支付密码
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("setPayPasswd")
    public AjaxJson setPayPasswd(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String mobile = req.getString("mobile");
        String code = req.getString("code");
        String payPsd = req.getString("payPsd");
        if (StringUtils.isBlank(mobile)) {
            ajaxJson.setMsg("手机号不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(code)) {
            ajaxJson.setMsg("验证码不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(payPsd)) {
            ajaxJson.setMsg("支付密码不能为空");
            return ajaxJson;
        }
        // 通过手机号查询该用户是否存在
        Member mobile1 = getMemberByMobile(mobile);
        if (Objects.isNull(mobile1)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        // 判断账户的启用状态
        if (mobile1.getState().equals("0")) {
            ajaxJson.setMsg("操作失败,账户已冻结");
            return ajaxJson;
        }
        // 查询验证码是否正确
        String s = redisTemplate.opsForValue().get(mobile);
        if (StringUtils.isBlank(s) || !s.equals(code)) {
            ajaxJson.setMsg("手机号或验证码错误");
            return ajaxJson;
        }
        // 设置支付密码
        mobile1.setPayPasswd(payPsd);
        memberService.save(mobile1);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("操作成功");
        return ajaxJson;
    }

    /**
     * 订单列表
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("orderList")
    public AjaxJson orderList(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String uid = req.getString("uid");
        String state = req.getString("state");
        Integer pageNo = req.getInteger("pageNo");
        Integer pageSize = req.getInteger("pageSize");
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        if (Objects.isNull(pageNo) || Objects.isNull(pageSize)) {
            ajaxJson.setMsg("分页参数不能为空");
            return ajaxJson;
        }
        MemberOrder memberOrder = new MemberOrder();
        memberOrder.setU(member);
        memberOrder.setOrderState(state);
        if (StringUtils.isBlank(state)) {
            memberOrder.setDataScope(" and a.order_state > 1");
        }
        Page<MemberOrder> page = memberOrderService.findPage(new Page<>(pageNo, pageSize), memberOrder);
        Map<String, Object> data = new ConcurrentHashMap<>();
        data.put("total", page.getLast());
        data.put("count", page.getCount());
        List<Map<String, Object>> dataList = new CopyOnWriteArrayList<>();
        for (MemberOrder order : page.getList()) {
            Map<String, Object> map = new ConcurrentHashMap<>();
            map.put("id", order.getId());
            map.put("title", order.getTitle());
            map.put("quantity", order.getQuantity());
            map.put("price", order.getPrice());
            map.put("money", order.getMoney());
            map.put("icon", getRealPath(order.getIcon()));
            map.put("orderState", order.getOrderState());
            map.put("surplusDate", 0);
            if (order.getOrderState().equals("3")) {
                map.put("surplusDate", getSurplusTime(order.getDeliveryTime()));
            }
            map.put("takeDate", Objects.isNull(order.getTakeDate()) ? "" : order.getTakeDate());
            dataList.add(map);
        }
        data.put("dateList", dataList);
        ajaxJson.put("data", data);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }


    /**
     * 计算剩余的毫秒值
     * @param date 开始日期
     * @return 剩余的毫秒值
     */
    public long getSurplusTime(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, 7);
        long start = new Date().getTime();// 当前时间的毫秒值
        long end = cal.getTime().getTime();// 结束时间的毫秒值
        return (end - start) < 0 ? 0 : (end - start);
    }

    /**
     * 订单详情
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("orderInfo")
    public AjaxJson orderInfo(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String orderId = req.getString("orderId");
        if (StringUtils.isBlank(orderId)) {
            ajaxJson.setMsg("订单id不能为空");
            return ajaxJson;
        }
        MemberOrder order = memberOrderService.get(orderId);
        if (Objects.isNull(order)) {
            ajaxJson.setMsg("该订单不存在");
            return ajaxJson;
        }
        Map<String, Object> data = new ConcurrentHashMap<>();
        data.put("id", order.getId());
        data.put("address", order.getAddress());
        data.put("title", order.getTitle());
        data.put("quantity", order.getQuantity());
        data.put("price", order.getPrice());
        data.put("money", order.getMoney());
        data.put("icon", getRealPath(order.getIcon()));
        data.put("orderState", order.getOrderState());
        data.put("createDate", ymdHms(order.getCreateDate()));
        data.put("deliveryTime", Integer.parseInt(order.getOrderState()) > 2 ? ymdHms(order.getDeliveryTime()) : "");
        data.put("takeDate", order.getOrderState().equals("4") ? ymdHms(order.getTakeDate()) : "");
        ajaxJson.put("data", data);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 确认收货
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("confirmOrder")
    public AjaxJson confirmOrder(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String orderId  = req.getString("orderId");
        if (StringUtils.isBlank(orderId)) {
            ajaxJson.setMsg("订单id不能为空");
            return ajaxJson;
        }
        MemberOrder order = memberOrderService.get(orderId);
        if (Objects.isNull(order)) {
            ajaxJson.setMsg("该订单不存在");
            return ajaxJson;
        }
        // 收货
        memberOrderService.confirmOrder(order);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("操作成功");
        return ajaxJson;
    }

    /**
     * 查看物流信息
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("viewExpress")
    public AjaxJson viewExpress(@RequestBody ReqJson req) throws Exception {
        AjaxJson ajaxJson = new AjaxJson();
        String orderId = req.getString("orderId");
        if (StringUtils.isBlank(orderId)) {
            ajaxJson.setMsg("订单id不能为空");
            return ajaxJson;
        }
        MemberOrder order = memberOrderService.get(orderId);
        if (Objects.isNull(order)) {
            ajaxJson.setMsg("该订单不存在");
            return ajaxJson;
        }
        // 查询快递公司的代号
        Express express = expressService.get(order.getExpress());
        net.sf.json.JSONObject kuaidi = KuaidiService.getKuaidi(express.getExpCode(), order.getExpNo(), "");
        kuaidi.put("expName", express.getTitle());
        kuaidi.put("expNo", order.getExpNo());
        kuaidi.put("deliveryTime", Objects.isNull(order.getDeliveryTime()) ? "" : ymdHms(order.getDeliveryTime()));
        ajaxJson.put("data", kuaidi);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 会员资料
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("myInfo")
    public AjaxJson myInfo(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String uid = req.getString("uid");
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        // 判断账户的启用状态
        if (member.getState().equals("0")) {
            ajaxJson.setMsg("操作失败,账户已冻结");
            return ajaxJson;
        }
        Map<String, Object> data = new HashMap<>();
        data.put("id", member.getId());
        data.put("nickname", member.getNickname());
        data.put("username", StringUtils.isBlank(member.getUsername()) ? "" : member.getUsername());
        data.put("photo", getRealPath(member.getPhoto()));
        data.put("code", member.getQrCode());
        data.put("mobile", member.getMobile());
        data.put("vipType", member.getVipType());
        data.put("tcPoint", member.getTcPoint());
        data.put("fcPoint", member.getFgPoint());
        data.put("masterReward", member.getMasterReward());
        data.put("payPasswdState", StringUtils.isBlank(member.getPayPasswd()) ? "0" : "1");
        ajaxJson.put("data", data);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 修改用户信息
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("editUserInfo")
    public AjaxJson editUserInfo(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String uid = req.getString("uid");
        String photo = req.getString("photo");
        String nickname = req.getString("nickname");
        String username = req.getString("username");
        String mobile = req.getString("mobile");
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        // 判断账户的启用状态
        if (member.getState().equals("0")) {
            ajaxJson.setMsg("操作失败,账户已冻结");
            return ajaxJson;
        }
        if (StringUtils.isBlank(mobile)) {
            ajaxJson.setMsg("手机号不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(nickname)) {
            ajaxJson.setMsg("昵称不能为空");
            return ajaxJson;
        }
        member.setPhoto(photo);
        member.setNickname(nickname);
        member.setUsername(username);
        member.setMobile(mobile);
        memberService.save(member);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("操作成功");
        return ajaxJson;
    }

    /**
     * 版本更新
     *
     * @return 状态
     */
    @RequestMapping("updateVersion")
    public AjaxJson updateVersion() {
        AjaxJson ajaxJson = new AjaxJson();
        Version version = new Version();
        List<Version> list = versionService.findList(version);
        if (CollectionUtils.isEmpty(list)) {
            ajaxJson.setMsg("未发现新版本");
            return ajaxJson;
        }
        List<Map<String, Object>> dataList = new CopyOnWriteArrayList<>();
        for (Version version1 : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("version", version1.getVersionNo());
            map.put("type", version1.getType());
            map.put("url", version1.getUrl());
            dataList.add(map);
        }
        ajaxJson.put("data", dataList);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 意见反馈
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("feedback")
    public AjaxJson feedback(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String uid = req.getString("uid");
        String content = req.getString("content");
        String mobile = req.getString("mobile");
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        Feedback feedback = new Feedback();
        feedback.setU(member);
        feedback.setMobile(mobile);
        feedback.setContent(content);
        feedbackService.save(feedback);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("操作成功");
        return ajaxJson;
    }

    /**
     * 关于我们
     *
     * @return 状态
     */
    @RequestMapping("aboutUs")
    public AjaxJson aboutUs() {
        AjaxJson ajaxJson = new AjaxJson();
        Page<AboutUs> page = aboutUsService.findPage(new Page<>(1, 1), new AboutUs());
        AboutUs aboutUs = page.getList().get(0);
        Map<String, Object> map = new ConcurrentHashMap<>();
        map.put("copyright", aboutUs.getCopyright());
        map.put("phone", aboutUs.getPhone());
        map.put("wechat", aboutUs.getWechat());
        map.put("qq", aboutUs.getQq());
        map.put("weibo", aboutUs.getWeibo());
        map.put("email", aboutUs.getEmail());
        map.put("url", aboutUs.getUrl());
        ajaxJson.put("data", map);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 常见问题
     *
     * @return 状态
     */
    @RequestMapping("faq")
    public AjaxJson faq() {
        AjaxJson ajaxJson = new AjaxJson();
        List<Faq> list = faqService.findList(new Faq());
        List<Map<String, Object>> data = new CopyOnWriteArrayList<>();
        for (Faq faq : list) {
            Map<String, Object> map = new ConcurrentHashMap<>();
            map.put("id", faq.getId());
            map.put("title", faq.getTitle());
            data.add(map);
        }
        ajaxJson.put("data", data);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }


    /**
     * 常见问题详情
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("faqInfo")
    public AjaxJson faqInfo(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String faqId = req.getString("faqId");
        if (StringUtils.isBlank(faqId)) {
            ajaxJson.setMsg("问题id不能为空");
            return ajaxJson;
        }
        Faq faq = faqService.get(faqId);
        ajaxJson.put("data", faq.getContent());
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }


    /**
     * 注销账号
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("closeAccount")
    public AjaxJson closeAccount(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String mobile = req.getString("mobile");
        String code = req.getString("code");
        if (StringUtils.isBlank(mobile)) {
            ajaxJson.setMsg("手机号不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(code)) {
            ajaxJson.setMsg("验证码不能为空");
            return ajaxJson;
        }
        Member member = getMemberByMobile(mobile);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        // 查询验证码是否正确
        String s = redisTemplate.opsForValue().get(mobile);
        if (StringUtils.isBlank(s) || !s.equals(code)) {
            ajaxJson.setMsg("手机号或验证码错误");
            return ajaxJson;
        }
        // 判断账户的启用状态
        if (member.getState().equals("0")) {
            ajaxJson.setMsg("操作失败,账户已冻结");
            return ajaxJson;
        }
        // 注销账号
        member.setMobile("");
        memberService.save(member);
        memberService.deleteByLogic(member);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("操作成功");
        return ajaxJson;
    }


    private BigDecimal convert2BigDecimal(String num) {
        return new BigDecimal(num).setScale(2, RoundingMode.HALF_UP);
    }

    /**
     * 查询我的积分
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("queryMyPoint")
    public AjaxJson queryMyPoint(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String uid = req.getString("uid");
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        ajaxJson.put("data", member.getTcPoint());
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 通过手机号查询名称
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("queryNameByMobile")
    public AjaxJson queryNameByMobile(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String mobile = req.getString("mobile");
        if (StringUtils.isBlank(mobile)) {
            ajaxJson.setMsg("手机号不能为空");
            return ajaxJson;
        }
        Member member = getMemberByMobile(mobile);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        ajaxJson.put("data", StringUtils.isBlank(member.getUsername()) ? member.getNickname() : "");
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 积分转出
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("rollOutPoint")
    public AjaxJson rollOutPoint(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String uid = req.getString("uid"); // 用户id
        String mobile = req.getString("mobile"); // 对方手机号
        Integer point = req.getInteger("point"); // 天畅积分
        String payPsd = req.getString("payPsd"); // 支付密码
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member out = memberService.get(uid);
        if (Objects.isNull(out)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        // 判断账户的启用状态
        if (out.getState().equals("0")) {
            ajaxJson.setMsg("操作失败,账户已冻结");
            return ajaxJson;
        }
        // 查询转出账户是否存在
        Member in = getMemberByMobile(mobile);
        if (Objects.isNull(in)) {
            ajaxJson.setMsg("转出用户不存在");
            return ajaxJson;
        }
        // 判断账户的支付密码是否正确
        if (!out.getPayPasswd().equals(payPsd)) {
            ajaxJson.setMsg("支付密码错误");
            return ajaxJson;
        }
        // 判断账户的天畅积分
        if (new BigDecimal(out.getTcPoint()).compareTo(new BigDecimal(point)) < 0) {
            ajaxJson.setMsg("天畅积分不足");
            return ajaxJson;
        }
//        if (out.getTcPoint() < point) {
//            ajaxJson.setMsg("天畅积分不足");
//            return ajaxJson;
//        }
        // 添加记录
        pointService.addRecord(in, out, point);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }


    /**
     * 积分转入记录
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("pointRecord")
    public AjaxJson pointRecord(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String uid = req.getString("uid");
        String type = req.getString("type");
        Integer pageNo = req.getInteger("pageNo");
        Integer pageSize = req.getInteger("pageSize");
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(type)) {
            ajaxJson.setMsg("类型不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        if (Objects.isNull(pageNo) || Objects.isNull(pageSize)) {
            ajaxJson.setMsg("分页参数不能为空");
            return ajaxJson;
        }
        Point point = new Point();
        String s = "";
        if (type.equals("0")) {
            s = "1";
        }
        if (type.equals("1")) {
            s = "2";
        }
        point.setPointType(s);
        if (s.equals("1")) {
            point.setInU(member);
        } else if (s.equals("2")) {
            point.setOutU(member);
        }
        Page<Point> page = pointService.findPage(new Page<>(pageNo, pageSize), point);
        Map<String, Object> data = new ConcurrentHashMap<>();
        data.put("total", page.getLast());
        data.put("count", page.getCount());
        List<Map<String, Object>> dataList = new CopyOnWriteArrayList<>();
        for (Point point1 : page.getList()) {
            Map<String, Object> map = new ConcurrentHashMap<>();
            if (s.equals("1")) {
                map.put("nickname", point1.getOutU().getNickname());
                map.put("mobile", point1.getOutU().getMobile());
                map.put("photo", getRealPath(point1.getOutU().getPhoto()));
                map.put("vipType", point1.getOutU().getVipType());
            }
            if (s.equals("2")) {
                map.put("nickname", point1.getInU().getNickname());
                map.put("mobile", point1.getInU().getMobile());
                map.put("photo", getRealPath(point1.getInU().getPhoto()));
                map.put("vipType", point1.getInU().getVipType());
            }
            map.put("point", point1.getMoney());
            map.put("createDate", ymd(point1.getCreateDate()));
            dataList.add(map);
        }
        data.put("dataList", dataList);
        ajaxJson.put("data", data);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 积分提现
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("withdraw")
    public AjaxJson withdraw(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String uid = req.getString("uid");
        String money = req.getString("money");
        String username = req.getString("username");
        String bank = req.getString("bank");
        String card = req.getString("card");
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        // 判断账户的启用状态
        if (member.getState().equals("0")) {
            ajaxJson.setMsg("操作失败,账户已冻结");
            return ajaxJson;
        }
        if (StringUtils.isBlank(money)) {
            ajaxJson.setMsg("金额不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(username)) {
            ajaxJson.setMsg("姓名不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(bank)) {
            ajaxJson.setMsg("银行不能为空");
            return ajaxJson;
        }
        if (StringUtils.isBlank(card)) {
            ajaxJson.setMsg("卡号不能为空");
            return ajaxJson;
        }
        // 判断账户的天畅积分
        if (new BigDecimal(member.getTcPoint()).compareTo(new BigDecimal(money)) < 0) {
            ajaxJson.setMsg("天畅积分不足");
            return ajaxJson;
        }
//        if (member.getTcPoint() < Integer.parseInt(money)) {
//            ajaxJson.setMsg("天畅积分不足");
//            return ajaxJson;
//        }
        // 提现比例查找
        Page<WithdrawInfo> page = withdrawInfoService.findPage(new Page<>(1, 1), new WithdrawInfo());
        WithdrawInfo withdrawInfo = page.getList().get(0);
        // 计算提现手续费
        BigDecimal bigDecimal = new BigDecimal(money).multiply(new BigDecimal(withdrawInfo.getRatio())).setScale(2, RoundingMode.HALF_UP);
        // 实际提现金额
        BigDecimal bigDecimal1 = new BigDecimal(money).subtract(bigDecimal).setScale(2, RoundingMode.HALF_UP);
        // 用户天畅积分扣减
        member.setTcPoint(new BigDecimal(member.getTcPoint()).subtract(new BigDecimal(money)).setScale(2, RoundingMode.HALF_UP).toString());
        Withdraw withdraw = new Withdraw();
        withdraw.setU(member);
        withdraw.setState("2");
        withdraw.setMoney(bigDecimal1.toString());
        withdraw.setUsername(username);
        withdraw.setBank(bank);
        withdraw.setCard(card);
        withdrawService.withdraw(withdraw, member);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }


    /**
     * 我的分享
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("myShare")
    public AjaxJson myShare(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String uid = req.getString("uid");
        Integer pageNo = req.getInteger("pageNo");
        Integer pageSize = req.getInteger("pageSize");
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        if (Objects.isNull(pageNo) || Objects.isNull(pageSize)) {
            ajaxJson.setMsg("分页参数不能为空");
            return ajaxJson;
        }
        Share share = new Share();
        share.setShare(member);
        Page<Share> page = shareService.findPage(new Page<>(pageNo, pageSize), share);
        Map<String, Object> data = new ConcurrentHashMap<>();
        data.put("total", page.getLast());
        data.put("count", page.getCount());
        List<Map<String, Object>> dataList = new CopyOnWriteArrayList<>();
        for (Share share1 : page.getList()) {
            Map<String, Object> map = new ConcurrentHashMap<>();
            map.put("photo", Objects.isNull(share1.getU()) ? "" : getRealPath(share1.getU().getPhoto()));
            map.put("nickname", Objects.isNull(share1.getU()) ? "" : (StringUtils.isBlank(share1.getU().getNickname()) ? "" : share1.getU().getNickname()));
            map.put("mobile", Objects.isNull(share1.getU()) ? "" : (StringUtils.isBlank(share1.getU().getMobile()) ? "" : share1.getU().getMobile()));
            map.put("createDate", ymd(share1.getCreateDate()));
            if (StringUtils.isNotBlank(map.get("mobile").toString())) {
                dataList.add(map);
            }
        }
        data.put("dataList", dataList);
        ajaxJson.put("data", data);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 提现说明
     *
     * @return 状态
     */
    @RequestMapping("withdrawInfo")
    public AjaxJson withdrawInfo() {
        AjaxJson ajaxJson = new AjaxJson();
        Page<WithdrawInfo> page = withdrawInfoService.findPage(new Page<>(1, 1), new WithdrawInfo());
        WithdrawInfo withdrawInfo = page.getList().get(0);
        ajaxJson.put("data",withdrawInfo.getRatio());
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }

    /**
     * 判断用户是否设置支付密码
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("getPayPsd")
    public AjaxJson getPayPsd(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String uid = req.getString("uid");
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        ajaxJson.put("data", StringUtils.isBlank(member.getPayPasswd()) ? "0" : "1");
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }




    /**
     * test
     *
     * @param req 参数
     * @return 状态
     */
    @RequestMapping("test")
    public AjaxJson test(@RequestBody ReqJson req) {
        AjaxJson ajaxJson = new AjaxJson();
        String uid = req.getString("uid");
        Integer pageNo = req.getInteger("pageNo");
        Integer pageSize = req.getInteger("pageSize");
        if (StringUtils.isBlank(uid)) {
            ajaxJson.setMsg("用户id不能为空");
            return ajaxJson;
        }
        Member member = memberService.get(uid);
        if (Objects.isNull(member)) {
            ajaxJson.setMsg("该用户不存在");
            return ajaxJson;
        }
        if (Objects.isNull(pageNo) || Objects.isNull(pageSize)) {
            ajaxJson.setMsg("分页参数不能为空");
            return ajaxJson;
        }
        ajaxJson.setCode(0);
        ajaxJson.setMsg("请求成功");
        return ajaxJson;
    }
}